<?php
/**
 * 服务窗应用激活开发者模式--应用网关
 * User: zym(wika)
 * Date: 2016/12/1
 * Time: 11:41
 */
class AlipayGatewayNAction extends CommonAction
{
    public $configArr;

    public function _initialize()
    {
        $this->configArr = array(
            'merchant_public_key' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxDaorN1R6Z2UAWUP8AFOFiREpqH5UwOvvkJIpD8wBbzDCL7KeO20eZIUpodoFEurMpGBN5W5fnS9xDwroq4VI5GnTWXLUy+x1cNEt8G5COPpNhsy++1yDRIdFGoSfp5jaz5bY1ue0sXKANDjboBoIrcW1t0riqqq6F1THIQxUuizlA/cmWy/II7ESXKlNpAj+kHS30BvYDegkCqBWCIQIzcd/jmR/If/zoCLTM5pF0VZlvUnLTkH4GfpOmi0iJ6ysOuKd2wHbWdqIilieiX1vd5X40jsvs1inclGxyEGzkMW1k1QLXIX2X+2sO5ruVzzuVxh4oCdnVXsStvlS4aRswIDAQAB",
            'merchant_private_key' => "MIIEpQIBAAKCAQEAxDaorN1R6Z2UAWUP8AFOFiREpqH5UwOvvkJIpD8wBbzDCL7KeO20eZIUpodoFEurMpGBN5W5fnS9xDwroq4VI5GnTWXLUy+x1cNEt8G5COPpNhsy++1yDRIdFGoSfp5jaz5bY1ue0sXKANDjboBoIrcW1t0riqqq6F1THIQxUuizlA/cmWy/II7ESXKlNpAj+kHS30BvYDegkCqBWCIQIzcd/jmR/If/zoCLTM5pF0VZlvUnLTkH4GfpOmi0iJ6ysOuKd2wHbWdqIilieiX1vd5X40jsvs1inclGxyEGzkMW1k1QLXIX2X+2sO5ruVzzuVxh4oCdnVXsStvlS4aRswIDAQABAoIBAQCC5pw9RyHAozn8fDnXumcn6mZKbMDzGwCCA6KGwQj+quHK0c4uQxcmC8jvGGLdN2YtRFlahe7MaIG5ixLpYw4DPsbIoeK1ypnvrGgJ15nkTnhlelvXKp/uDGmOFv3jrzNQdrdtX9BjeUS8qp8wN0ZB8rev/Llxt05x2uD5o09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mnt/lost+found/www/pos_pay.xiaotui.so/upload/alipay/alipay_rsa_public_key.pem",
            'merchant_private_key_file' => "/mnt/lost+found/www/pos_pay.xiaotui.so/upload/alipay/rsa_private_key.pem",
            'merchant_public_key_file' => "/mnt/lost+found/www/pos_pay.xiaotui.so/upload/alipay/rsa_public_key.pem",*/
            'sign_type' => "RSA2",
            'charset' => "GBK",
            'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
            'app_id' => "2017040506553849"
        );
    }

    /**
     * 此文件未对接支付宝服务器的网关文件，将此文件的访问路径填入支付宝服务窗的开发中验证网关的页面中。
     * 次文件接收支付宝服务器发送的请求
     */
    public function index()
    {
        if (get_magic_quotes_gpc()) {
            foreach ($_POST as $key => $value) {
                $_POST [$key] = stripslashes($value);
            }

            foreach ($_GET as $key => $value) {
                $_GET [$key] = stripslashes($value);
            }
            foreach ($_REQUEST as $key => $value) {
                $_REQUEST [$key] = stripslashes($value);
            }
        }
        // 日志记录下受到的请求
        wLog("POST: " . var_export($_POST, true), 'getway');
        wLog("GET: " . var_export($_GET, true), 'getway');
        $sign = $this->_request('sign');
        $sign_type = $this->_request('sign_type');
        $biz_content = $this->_request('biz_content');
        $service = $this->_request('service');
        $charset = $this->_request('charset');
        if (empty ($sign) || empty ($sign_type) || empty ($biz_content) || empty ($service) || empty ($charset)) {
            echo "some parameter is empty.";
            exit();
        }
        // 收到请求，先验证签名
        vendor("Alipay.AlipaySignN");
        $as = new AlipaySignN();
//        $sign_verify = $as->rsaCheckV2($_POST, $this->configArr ['alipay_public_key'], $this->configArr['sign_type']);
        $this->verifygw(true, $as);
//        if (!$sign_verify) {
//            wLog("sign qianming verfiy fail.", 'getway');
//            // 如果验证网关时，请求参数签名失败，则按照标准格式返回，方便在服务窗后台查看。
//            if ($this->_request("service") == "alipay.service.check") {
//                $this->verifygw(false, $as);
//            } else {
//                echo "sign verfiy fail.";
//            }
//            exit ();
//        }
//
//        // 验证网关请求
//        if ($this->_request("service") == "alipay.service.check") {
//            // Gateway::verifygw();
//            $this->verifygw(true, $as);
//        }
    }


    public function verifygw($is_sign_success, $as)
    {
        $as->rsaPrivateKey = $this->configArr['merchant_private_key'];
        // global $this->configArr;
        // print_r ( $this->configArr );
        if ($is_sign_success) {
            $response_xml = "<success>true</success><biz_content>" . $this->configArr ['merchant_public_key'] . "</biz_content>";
        } else { // echo $response_xml;
            $response_xml = "<success>false</success><error_code>VERIFY_FAILED</error_code><biz_content>" . $this->configArr ['merchant_public_key'] . "</biz_content>";
        }

        $mysign = $as->alonersaSign($response_xml, $this->configArr['merchant_private_key'], $this->configArr['sign_type']);
        $return_xml = "<?xml version=\"1.0\" encoding=\"" . $this->configArr['charset'] . "\"?><alipay><response>" . $response_xml . "</response><sign>" . $mysign . "</sign><sign_type>" . $this->configArr['sign_type'] . "</sign_type></alipay>";
        wLog("return_xml:" . $return_xml, 'getway');
        echo $return_xml;
        exit ();
    }
}

